/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.seu.cose.hobbysharing.dao;

import cn.edu.seu.cose.hobbysharing.bean.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.LinkedList;
import java.util.UUID;

/**
 *
 * @author Yan
 */
public class UserDao {
    private ConnectionFactory f ;
    private static String getUuidKey() {
        return UUID.randomUUID().toString();
    }
    private User getUserByResultSet (ResultSet rs) throws SQLException{
        String user_uuid = rs.getString("user_uuid");
        String user_display_name = rs.getString("user_display_name");
        String user_blog_url = rs.getString("user_blog_url");
        String user_email = rs.getString("user_email");
        String user_home_page = rs.getString("user_home_page_url");
        User user = new User ();
        user.setUserUuid(user_uuid);
        user.setUserDisplayName(user_display_name);
        user.setUserBlogUrl(user_blog_url);
        user.setUserEmail(user_email);
        user.setHomePageUrl(user_home_page);
        return user;
    }
    public UserDao (){
        f = new ConnectionFactory ();
    }
    public User getUserByID (String user_uuid) throws SQLException{
        Connection c =  f.getConnection();
        try{
        
        PreparedStatement ps =c.prepareStatement("select * from hs_users where user_uuid = ?");
        ps.setString(1, user_uuid);
        ResultSet rs = ps.executeQuery();
        if (!rs.next()){
            return null;
        }
        return getUserByResultSet(rs);
        }finally {
            c.close();
        }
    }
    public Collection<User> getUsers() throws SQLException{
        Connection c =f.getConnection();
        try {
        PreparedStatement ps =c .prepareStatement("select * from hs_users");
        //ps.setString(1, );
        ResultSet rs = ps.executeQuery();
        Collection <User> users = new LinkedList<User>();
        while (rs.next()){
            users.add(getUserByResultSet(rs));
        }
        return users;
        }finally {
            c.close();
        }
     }
     public String addUser (String user_display_name,String user_blog_url,String user_email,String user_home_page_url) throws SQLException{
        Connection c = f.getConnection();
        try {
        PreparedStatement ps = c.prepareStatement("insert into hs_users(user_uuid,user_display_name"
                +",user_blog_url,user_email,user_home_page_url) values (?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS);
        String key = getUuidKey();
        ps.setString(1,key);
        ps.setString(2,user_display_name);
        ps.setString(3,user_blog_url);
        ps.setString(4,user_email);
        ps.setString(5,user_home_page_url);
        int number = ps.executeUpdate();
        if (number<=0){
            return null;
        }
        return key;
        }finally {
            c.close();
        }
    } 
    public String addUser (String user_display_name,String user_blog_url,String user_email) throws SQLException{
        Connection c = f.getConnection();
        try {
        PreparedStatement ps = c.prepareStatement("insert into hs_users(user_uuid,user_display_name"
                +",user_blog_url,user_email) values (?,?,?,?)", Statement.RETURN_GENERATED_KEYS);
        String key = getUuidKey();
        ps.setString(1,key);
        ps.setString(2,user_display_name);
        ps.setString(3,user_blog_url);
        ps.setString(4,user_email);
        int number = ps.executeUpdate();
        if (number<=0){
            return null;
        }
        return key;//告诉新用户的ID
        }finally {
            c.close();
        }
    }    
    public void deleteUser (String user_uuid) throws SQLException{
        Connection c = f.getConnection();
        try {
        PreparedStatement ps = c.prepareStatement("delete from hs_users where user_uuid=?");
        ps.setString(1, user_uuid);
        ps.executeUpdate();
        }
        finally {
            c.close();
        }
    }
    public void editUser (String user_uuid,String user_display_name,String user_blog_url,String user_email,String user_home_page_url) throws SQLException{
        Connection c = f.getConnection();
        try {
        PreparedStatement ps = c.prepareStatement("update hs_users set user_display_name=?,user_blog_url=?,"
                + "user_email=?,user_home_page_url=? where user_uuid =?");
        ps.setString(1,user_display_name);
        ps.setString(2,user_blog_url);
        ps.setString(3,user_email);
        ps.setString(4,user_home_page_url);
        ps.setString(5, user_uuid);
        ps.executeUpdate();
        }finally {
            c.close();
        }
    }    
    public boolean existUrl (String user_blog_url) throws SQLException{
        Connection c = f.getConnection();
        try {
        PreparedStatement ps = c.prepareStatement("select * from hs_users where user_blog_url=? limit 1");
        ps.setString(1, user_blog_url);
        ResultSet rs = ps.executeQuery();
        return rs.next();
        }
        finally {
            if (c!=null)
            c.close();
        }
    }
    public String getUuidByUserBlogUrl (String user_blog_url) throws SQLException{
        Connection c = f.getConnection();
        try {
        PreparedStatement ps = c.prepareStatement("select * from hs_users where user_blog_url=? ");
        ps.setString(1, user_blog_url);
        ResultSet rs = ps.executeQuery();
        if (!rs.next()){
            return null;
        }
        return rs.getString(1);
        }
        finally {
            c.close();
        }
    }
    
}


